package com.neusoft.a132wxyorderingdish.fragments;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.neusoft.a132wxyorderingdish.R;
import com.neusoft.a132wxyorderingdish.activity.SearchResultActivity;
import com.neusoft.a132wxyorderingdish.beans.FoodBean;
import com.neusoft.a132wxyorderingdish.db.RecordDBOperation;
import com.neusoft.a132wxyorderingdish.db.RecordSQLiteOpenHelper;

import java.util.List;

public class SearchFragment extends BaseFragment {


    private TextView tv_his,tv_del;
    private ImageButton imageButton;
    private EditText tv_search;
    private ListView listView;
    public RecordDBOperation recordDB;
    public RecordSQLiteOpenHelper helper;
    private FoodBean foodBean;
    private String  tempName;

    @Override
    void initView() {
        tv_search=view.findViewById(R.id.et_search);
        imageButton=view.findViewById(R.id.btn_search);
        tv_his=view.findViewById(R.id.tv_tip);
        tv_del=view.findViewById(R.id.tv_clear);
        listView=view.findViewById(R.id.listView);
    }

    @Override
    void initEvent() {
        //清空搜索历史
        tv_del.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordDB.deleteData();
                showData("");

            }
        });
        imageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordDB.insertData(tv_search.getText().toString().trim());
                showDetailInfo(tv_search.getText().toString().trim());
            }
        });

        //搜索框的键盘搜索键点击回调
        tv_search.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
                if(keyCode==keyEvent.KEYCODE_ENTER&&keyEvent.getAction()==KeyEvent.ACTION_DOWN){//修改回车键功能
                    ((InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE)).hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
                    boolean hasData=recordDB.hasData(tv_search.getText().toString().trim());
                    if(!hasData){
                        recordDB.insertData(tv_search.getText().toString().trim());
                        showData("");
                    }
                    showDetailInfo(tv_search.getText().toString().trim());
                }
                return false;
            }
        });

        //搜索框的文本变化实时监听
        tv_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                imageButton.setVisibility(View.VISIBLE);
                if(editable.toString().trim().length()==0){
                    tv_his.setText("搜索历史");
                }
                else {
                    tv_his.setText("搜索结果");
                }
                tempName=tv_search.getText().toString();
                showData(tempName);
            }
        });

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
                TextView textView=(TextView)view.findViewById(android.R.id.text1);
                String name=textView.getText().toString();
                tv_search.setText(name);
                showDetailInfo(name);
            }
        });
    }



    @Override
    void initData() {

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        setLayout_file(R.layout.fragment_search);
        helper = new RecordSQLiteOpenHelper(context);
        recordDB=new RecordDBOperation(helper);
        return super.onCreateView(inflater, container,savedInstanceState);
    }

    void showData(String tempName){
        List list=recordDB.queryData(tempName);
        //创建适配器对象
        SimpleAdapter adapter=new SimpleAdapter(context,list,android.R.layout.simple_list_item_1,new String[]{"name"},new int[]{android.R.id.text1});
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }

    void showDetailInfo(String name){
        //跳转到搜索结果处理页面
        Intent intent=new Intent(getActivity(), SearchResultActivity.class);
        intent.putExtra("search",name);
        startActivity(intent);
    }

    @Override
    public void onResume(){
        super.onResume();
        helper=new RecordSQLiteOpenHelper(context);
        recordDB=new RecordDBOperation(helper);
    }


}
